package msu.ml.data;

import msu.ml.core.*;
import msu.ml.core.NxInstances;
import weka.filters.*;

/**
 * Provides an adapter for using the Weka Discretize filter in the Nexrad Explorer
 * Software.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class DiscretizationPreProcessor 
   extends weka.filters.unsupervised.attribute.Discretize implements IDataPreProcessor 
{

	public DiscretizationPreProcessor()
	{

	}

	/**
	 * Process data by discretizing attributes
	 * @param data data to process
	 * @return processed data
	 */
	public NxInstances process(NxInstances data)
	{
      try
      {
         String numericAttrs = "";
         for(int i = 0; i < data.numAttributes(); i++)
         {
            if(data.attribute(i).isNumeric())
               numericAttrs += (numericAttrs.length() == 0) ? ""+(i+1) 
                  : "," + (i+1);
         }
         if(numericAttrs.length() > 0)
         {
            this.setAttributeIndices(numericAttrs);
            this.setInputFormat(data);
         }

         NxInstances result = new NxInstances(Filter.useFilter(data, this));
         result.setClassIndex(result.numAttributes() - 1);
         result.setSourceFile(data.getSourceFile());
         return result;
      }
      catch(Exception e)
      {
         System.out.println("Discretization Error");
         System.out.println(e.getMessage());
         System.out.println(e.getStackTrace());
         return null;
      }
	}

    public String toString()
    {
        return "<discretization_pre_processor />";
    }

}
